<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\HouseAudit;
use App\Models\AddressModel;
use App\Models\HouseList;
use App\Models\HouseTypeInfo;
use App\Services\ElasticSearchService;
use App\Services\RedisGeo;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Godruoyi\Snowflake\Snowflake;

class HouseAuditController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new HouseAudit(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('house_logo','图片')->image();
            $grid->column('house_name','小区名称');
            $grid->column('house_type','房源类型')->display(function ($house_type){
              $name= HouseTypeInfo::where('id',$house_type)->first();
                return $name['house_type_name'];
            });
            $grid->column('house_area','区域');
//            $grid->column('house_moldings','房源户型');
            $grid->column('house_floor','楼层');
            $grid->column('house_acreage','房源面积');
            $grid->column('house_price','售价');
            $grid->column('house_title','房源标题');
            $grid->column('house_text','简介');
            $grid->column('audit_status','审核状态')->radio([0=>'正在审核',1=>'审核通过',2=>'拒绝']);
            $grid->column('square_price','平方价格');
            $grid->column('house_way','入住方式')->display(function ($house_way){
                if($house_way==1){
                    return '买房';
                }else{
                    return '租房';
                }
            });
            $grid->column('house_address','房源地址');
            $grid->column('uid','用户id');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HouseAudit(), function (Show $show) {
            $show->field('id');
            $show->field('house_logo','图片')->image();
            $show->field('house_name','小区名称');
            $show->field('house_type','房源类型');
            $show->field('house_area','区域');
//            $show->field('house_moldings','房源户型');
            $show->field('house_floor','楼层');
            $show->field('house_acreage','房源面积');
            $show->field('house_price','售价');
            $show->field('house_title','房源标题');
            $show->field('house_text','简介');
            $show->field('audit_status','审核状态')->using([0=>'正在审核',1=>'审核通过',2=>'拒绝']);
            $show->field('square_price','平方价格');
            $show->field('house_way','入住方式')->using([1=>'买房',2=>'租房']);
            $show->field('house_address','房源地址');
            $show->field('uid','用户id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HouseAudit(), function (Form $form) {
            $form->display('id');
            $form->image('house_logo','图片')->autoUpload();
            $form->text('house_name','小区名称');
            $form->text('house_type','房源类型');
            $form->text('house_city','城市');
            $form->text('house_area','区域');
            $form->text('house_moldings','房源户型');
            $form->text('house_floor','楼层');
            $form->text('house_acreage','房源面积');
            $form->text('house_price','售价');
            $form->text('house_title','房源标题');
            $form->text('house_text','简介');
            $form->radio('audit_status','审核状态')->options([0=>'正在审核',1=>'审核通过',2=>'拒绝']);
            $form->text('square_price','平方价格');
            $form->text('house_way','入住方式')->options([1=>'买房',2=>'租房']);
            $form->text('house_address','房源地址');
            $form->hidden('house_number','编号');
            $form->hidden('house_latitude','经度');
            $form->hidden('house_longitude','纬度');
            $form->text('uid','用户id');
            $form->display('created_at');
            $form->display('updated_at');

            $form->select('province','省级')->options('province')->load('city','city');
            $form->select('city','市级')->load('town','town');
            $form->select('town','区级');

            $form->submitted(function (Form $form) {
                // 获取用户提交参数
                $province = $form->province;
                $city = $form->city;
                $town = $form->town;
                // 上面写法等同于
                // 删除用户提交的数据
                $form->deleteInput('province');
                $form->deleteInput('city');
                $form->deleteInput('town');
                if ($province&&$city&&$town){
                    $address=new AddressModel();
                    $province=$address->where('id',$province)->select('name')->get()->toarray();
                    $province=$province[0]['name'];
                    $city=$address->where('id',$city)->select('name')->get()->toarray()[0]['name'];
                    $town=$address->where('id',$town)->select('name')->get()->toarray()[0]['name'];
//                    $form->house_area=$province.$city.$town;
                    $form->house_area=$town;
                    $form->house_city=$province;
                }
            });

            $form->saving(function (Form $form){
                if ($form->isCreating()){
                    $form->house_number=(new Snowflake())->id();
                }
            });
            $form->saved(function (Form $form){
                if ($form->isEditing()) {
                    $state=$this->audit_status;
                    $res= HouseList::where('house_number',$this->house_number)->first();
                    if ($state==1 && !$res)
                    {
                            $resListInfo=HouseList::create([
                                'house_logo'=>$this->house_logo,
                                'house_name'=>$this->house_name,
                                'house_type'=>$this->house_type,
                                'house_city'=>$this->house_city,
                                'house_area'=>$this->house_area,
                                'house_moldings'=>$this->house_moldings,
                                'house_floor'=>$this->house_floor,
                                'house_acreage'=>$this->house_acreage,
                                'house_price'=>$this->house_price,
                                'house_title'=>$this->house_title,
                                'house_text'=>$this->house_text,
                                'square_price'=>$this->square_price,
                                'house_way'=>$this->house_way,
                                'status'=>1,
                                'house_address'=>$this->house_address,
                                'house_number'=>$this->house_number,
                                 'uid'=>$this->uid,
                                'house_latitude'=>$this->house_latitude,
                                'house_longitude'=>$this->house_longitude
                            ]);

                        }else{
                        return ;
                    }


                }
            });
        });
    }
}
